﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.Windows.Forms;

namespace QLQTPM_Nhom6.UTIL
{
    public class DataProvider
    {
       // public string strConnect = ConfigurationManager.ConnectionStrings["conString"].ConnectionString.ToString();
      
        #region Properties
        private OleDbConnection _OleDbConn;

        public OleDbConnection OleDbConn
        {
            get { return _OleDbConn; }
            set { _OleDbConn = value; }
        }
        private OleDbCommand _OleDbComm;

        public OleDbCommand OleDbComm
        {
            get { return _OleDbComm; }
            set { _OleDbComm = value; }
        }
        private OleDbDataAdapter _OleDbDataAdapter;

        public OleDbDataAdapter OleDbD_Adapter
        {
            get { return _OleDbDataAdapter; }
            set { _OleDbDataAdapter = value; }
        }
        private OleDbDataReader _OleDbDataReader;

        public OleDbDataReader OleDbD_Reader
        {
            get { return _OleDbDataReader; }
            set { _OleDbDataReader = value; }
        }
        private DataTable _DataTable;

        public DataTable Data_Table
        {
            get { return _DataTable; }
            set { _DataTable = value; }
        }

        #endregion

        #region Connect/DisConnect
        public bool Open()
        {
            try
            {
                // File database lưu cùng file .exe
               String strConnect = "Provider = Microsoft.ACE.OLEDB.12.0 ;" +
                                "Data Source =" + Application.StartupPath + @"\Database_Nhom6.accdb;" +
                                "Jet OLEDB:Database Password = '';" +
                                "User ID = admin";      
                this._OleDbConn = new OleDbConnection(strConnect);
                this._OleDbConn.Open();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
                return false;
            }
            return true;
        }
        public void DisConn()
        {
            try
            {
                if (this._OleDbConn != null)
                {
                    if (this._OleDbConn.State == ConnectionState.Open)
                        OleDbConn.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
        }
        #endregion

        #region Xử Lý
        //phuong thuc Lay Du Lieu
        public DataTable ExecuteQuery(string sql)
        {
            Data_Table = new DataTable();
            OleDbD_Adapter = new OleDbDataAdapter(sql, OleDbConn);
            OleDbD_Adapter.Fill(Data_Table);
            return Data_Table;
        }

        // phuong thuc Cap nhat thay doi tren Du lieu
        // them , xoa, sua
        public void ExecuteNonQuery(string sql)
        {
            OleDbComm = new OleDbCommand();
            OleDbComm.CommandText = sql;
            OleDbComm.Connection = OleDbConn;
            OleDbComm.ExecuteNonQuery();
        }
        #endregion
    }
}
